//
//  XHUserAccountTools.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/28.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit

class XHUserAccountTools: NSObject {
    
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    var userAccount:XHUserAccount?
    //判断用户是否登录
    var isLogin:Bool{
       return accessToken != nil
    }
    
    //访问令牌 access——token(只读)
    var accessToken:String?{
        if userAccount?.access_token == nil{
            return nil
        }else{
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userAccount?.access_token
            }else{
                return nil
            }
        }
    }
    
    
    
    static let sharedTools:XHUserAccountTools = XHUserAccountTools()
    
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
    
    func saveUserAccount(userAccount:XHUserAccount){
      self.userAccount = userAccount
          
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
        
    }
    //获得对象
    
    func getUserAccount() ->XHUserAccount?{
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
        
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? XHUserAccount
        
        return result
    }
    
    
}
